package huixue.cn.com.rxapp.stock.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import java.util.ArrayList;

import huixue.cn.com.library.util.LogUtils;
import huixue.cn.com.rxapp.stock.Renderer;
import huixue.cn.com.rxapp.widget.data.EntryData;
import huixue.cn.com.rxapp.widget.data.StickData;

/**
 * Created by Administrator on 2018/8/19.
 */

public class KLine3View extends View {

    private RectF contentRect;

    private float contentMinOffset;

    private Renderer renderer;

    //K线所有数据
    private EntryData mData;

    public KLine3View(Context context) {
        this(context, null);
    }

    public KLine3View(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mData = new EntryData();
        //关闭硬件加速，不然虚线显示为实线了
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        contentRect = new RectF();
        contentMinOffset = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0, context.getResources().getDisplayMetrics());

        renderer = new Renderer();
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        contentRect.set(contentMinOffset * 2, contentMinOffset, w - contentMinOffset, h - contentMinOffset);
        notifyDataSetChanged(false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        renderer.render(canvas);
    }


    private void notifyDataSetChanged(boolean invalidate){
        mData.calcMinMax(0, mData.entries.size());
        renderer.setContentRect(contentRect);
        renderer.setData(mData);
    }

    public void setDataAndInvalidate(ArrayList<StickData> data) {
        for(int i= 0;i<data.size();i++){
            mData.addEntry(data.get(i));
        }
    }
}
